package com.ymtms.customerservice.controller;

import com.ymtms.customerservice.pojo.ComplaintInformation;
import com.ymtms.customerservice.pojo.CustomerInformation;
import com.ymtms.customerservice.pojo.TotalFreight;
import com.ymtms.customerservice.service.CustomerInformationService;
import com.ymtms.customerservice.util.TimeRangeUtil;
import com.ymtms.customerservice.util.ValidateUtil;
import com.ymtms.customerservice.vo.GridRequest;
import com.ymtms.customerservice.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@CrossOrigin
@RequestMapping("/customerinformation")
public class CustomerInformationController {
    @Autowired
    private CustomerInformationService customerInformationService;

    /**
     * 已由其他替代
     * @return
     */
    @RequestMapping("/moneytest")
    @ResponseBody
    public Map moneyTest(){
        Map map = new HashMap();
//        List<String> sendGoodsCustomers=customerInformationService.selectAllCarriageRankingCustomer();
//        List<BigDecimal> allCarriages=customerInformationService.selectAllCarriageRankingMoney();

//        String [] sendGoodsCustomers={"哈哈","小可爱","凹凸曼","可爱多","娃哈哈","小霸王","营养快线"};
//        int[] allCarriages= {33     ,44     ,55     ,66     ,77     ,88        ,99         };
//        map.put("sendGoodsCustomers",sendGoodsCustomers);
//        map.put("allCarriages",allCarriages);

        List<TotalFreight> totalFreights = customerInformationService.selectAllCarriageRanking();
        System.out.println(totalFreights);
        map.put("totalFreights",totalFreights);
        return map;
    }

    /**
     * 管理员查看页面
     * 获取所有客户编号
     * @return
     */
    @RequestMapping("/getcustomercode")
    @ResponseBody
    public List<String> getCustomerCode(){
        List<String> customerCodes=new LinkedList<>();
        customerCodes= customerInformationService.getCustomercode();
        return customerCodes;
    }
    /**
     * 管理员查看页面
     * 查看所有客户
     * @param customerInformation
     * @return
     */
    @RequestMapping("/findcustomer")
    @ResponseBody
    public Map findCustomer(CustomerInformation customerInformation){
        Map map = new HashMap();
        List<CustomerInformation> customerInformations = customerInformationService.findCustomer(customerInformation);
        map.put("customerInformations",customerInformations);
        return map;
    }

    /**
     * layui
     * 查看所有客户（带条件）
     * @param gridRequest
     * @param customerInformation
     * @return
     */
    @RequestMapping("/findcustomerall")
    @ResponseBody
    public GridResult<CustomerInformation> cmblist(GridRequest gridRequest, /* 封装页面中laiyu表格所传过来的基本信息*/
                                                   CustomerInformation customerInformation/*实体类接收非范围参数*/){

        System.out.println("gridRequest:"+gridRequest);
        System.out.println("customerInformation:"+customerInformation);
        GridResult<CustomerInformation> result = customerInformationService.queryByPage(
                gridRequest, customerInformation);
//        GridResult<CollectingMoneyBalance> result = collectingMoneyBalanceService.queryByPage(Integer.parseInt(params.get("page")), Integer.parseInt(params.get("limit")));
//        GridResult<CollectingMoneyBalance> result = collectingMoneyBalanceService.queryByPage(1,10);
        result.setCode(0);
        result.setMsg("");
        System.out.println(result);
        return result;
    }



    /**
     * 插入客户
     * @param customerInformation
     * @return
     */
    @RequestMapping("/fillin")
    @ResponseBody
    public Map fillInCustomerInformation(CustomerInformation customerInformation){
//        System.out.println("↓↓↓↓↓传参验证↓↓↓↓↓");
//        System.out.println(customerInformation);
//        System.out.println("↑↑↑↑↑传参验证↑↑↑↑↑");
        Map map = new HashMap();

        //记得生成一个客户编号
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        System.out.println(uuid);
        String result="添加失败";
       /* if (customerInformation.getCustomerCode() == null || customerInformation.getCustomerCode() == "") {
            result="客户编号不可为空";
            map.put("msg",result);
            return map;
        }*/
        if (customerInformation.getCustomer() == null || customerInformation.getCustomer() == "") {
            result="客户名称不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getPhone() == null || customerInformation.getPhone() == "") {
            result="电话不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getAddress() == null || customerInformation.getAddress() == "") {
            result="地址不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getPostCode() == null || customerInformation.getPostCode() == "") {
            result="邮编不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getLinKman() == null || customerInformation.getLinKman() == "") {
            result="联系人不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getLinkmanMobile() == null || customerInformation.getLinkmanMobile() == "") {
            result="联系人手机不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getCustomerType() == null || customerInformation.getCustomerType() == "") {
            result="客户类型不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getEnterpriseProperty() == null || customerInformation.getEnterpriseProperty() == "") {
            result="企业性质不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getEnterpriseSize() == null || customerInformation.getEnterpriseSize() == "") {
            result="企业规模不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getEmail() == null || customerInformation.getEmail() == "") {
            result="电子邮件不可为空";
            map.put("msg",result);
            return map;
        }


        customerInformation.setCustomerCode(uuid);
        result=customerInformationService.fillInCustomerInformation(customerInformation);
        map.put("msg",result);
        return map;
    }

    /**
     * 修改客户信息
     * @param customerInformation
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public Map customerModify(CustomerInformation customerInformation){
        Map map = new HashMap();
        String result="修改失败";
        if (customerInformation.getCustomer() == null || customerInformation.getCustomer() == "") {
            result="客户名称不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getPhone() == null || customerInformation.getPhone() == "") {
            result="电话不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getAddress() == null || customerInformation.getAddress() == "") {
            result="地址不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getPostCode() == null || customerInformation.getPostCode() == "") {
            result="邮编不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getLinKman() == null || customerInformation.getLinKman() == "") {
            result="联系人不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getLinkmanMobile() == null || customerInformation.getLinkmanMobile() == "") {
            result="联系人手机不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getCustomerType() == null || customerInformation.getCustomerType() == "") {
            result="客户类型不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getEnterpriseProperty() == null || customerInformation.getEnterpriseProperty() == "") {
            result="企业性质不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getEnterpriseSize() == null || customerInformation.getEnterpriseSize() == "") {
            result="企业规模不可为空";
            map.put("msg",result);
            return map;
        }
        if (customerInformation.getEmail() == null || customerInformation.getEmail() == "") {
            result="电子邮件不可为空";
            map.put("msg",result);
            return map;
        }


        result=customerInformationService.customerModify(customerInformation);
        map.put("msg",result);
        return map;
    }

    /**
     * 删除
     * @param customerCode
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteCustomer(String customerCode){
        Map map = new HashMap();
        String result=customerInformationService.deleteCustomer(customerCode);
        map.put("msg",result);
        return map;
    }







}
